﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DIS_Client.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using DIS_Client.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	using DIS_Client.DIS_BUS_WS;
	#endregion
	public partial class FrmViewDailyReport : Office2007Form
	{
		private DevComponents.DotNetBar.Controls.TextBoxX txtIDRestaurant;

        public FrmViewDailyReport(bool enableChon = false)
		{
			InitializeComponent();
		}

		private void FrmManageDailyReport_Load(object sender, EventArgs e)
		{
			InitGridView();
			InitHiddenFields();
		}

		public void LoadDetailInfo(DailyReportDTO item)
		{
			txtIDRestaurant.Text = item.IDRestaurant == -1 ? "" : item.IDRestaurant.ToString();
			dtReportDate.Value = item.ReportDate;
		}

		private void InitGridView()
		{
            DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "ID",
                DataPropertyName = "ID",
                Name = "ID",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 4))
            };
            dtgvData.Columns.Add(objColumn);
            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "IsUsed",
                DataPropertyName = "IsUsed",
                Name = "IsUsed",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 4))
            };
            dtgvData.Columns.Add(objColumn);
            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "IsPaid",
                DataPropertyName = "IsPaid",
                Name = "IsPaid",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 4)) - 1
            };
            dtgvData.Columns.Add(objColumn);
            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "Total",
                DataPropertyName = "Total",
                Name = "Total",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 4)) - 1
            };
            dtgvData.Columns.Add(objColumn);
		}

		public void InitHiddenFields()
		{
			txtIDRestaurant = new DevComponents.DotNetBar.Controls.TextBoxX
			{
				Name = "txtIDRestaurant",
				Text = ""
			};
			txtIDRestaurant.Visible = false;
		}

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSelectRestaurant_Click(object sender, EventArgs e)
        {
            FrmManageRestaurant frm = new FrmManageRestaurant(true);
            frm.Handler += GetRestaurant;
            frm.ShowDialog();
        }

        public void GetRestaurant(object sender, EventArgs e)
        {
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });
            txtIDRestaurant.Text = comp[0];
            txtRestaurant.Text = comp[1];
        }

        private void btnViewReport_Click(object sender, EventArgs e)
        {
            var errorText = "";
			if (!ValidateInput(ref errorText))
			{
				MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
            var report = BusService.Instance.DailyReport_SelectByIDRestaurantAndReportDate(int.Parse(txtIDRestaurant.Text), dtReportDate.Value);
            if (report != null)
            {
                var lstItem = BusService.Instance.Orders_SelectByIDRestaurantAndOrderDate(int.Parse(txtIDRestaurant.Text), dtReportDate.Value);
                var dt = new DataTable();
                dt.Columns.Add("ID");
                dt.Columns.Add("IsUsed");
                dt.Columns.Add("IsPaid");
                dt.Columns.Add("Total");
                var income = 0.0;
                for (int i = 0; i < lstItem.Length; i++)
                {
                    var lstOrderDetails = lstItem[i].OrderDetails;
                    var sum = 0.0;
                    foreach (var detail in lstOrderDetails)
                    {
                        var money = detail.Quantity * detail.Price;
                        var remain = money - money * (detail.Discount/100);
                        sum += remain;
                    }
                    income += sum;
                    dt.Rows.Add(lstItem[i].ID, lstItem[i].IsUsed == true ? "Yes" : "No", lstItem[i].IsPaid == true ? "Yes" : "No", sum);
                }
                dt.Rows.Add("", "", "SUM", income);
                DataView objTimeSheetDV = new System.Data.DataView(dt);
                objTimeSheetDV.AllowNew = false;
                dtgvData.AutoGenerateColumns = false;
                dtgvData.DataSource = objTimeSheetDV;
                dtgvData.ClearSelection();
            }
            else
            {
                MessageBox.Show("There is no any reports created in this date", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool ValidateInput(ref string errorText)
        {
            if (txtIDRestaurant.Text == "")
            {
                errorText = "Please select restaurant";
                return false;
            }
            if (dtReportDate.Value == DateTime.MinValue)
            {
                errorText = "Please select ReportDate";
                return false;
            }
            return true;
        }

        private void dtgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgvData.SelectedRows.Count > 0 && dtgvData.SelectedRows[0].Cells[2].Value.ToString().ToUpper() != "SUM")
            {
                FrmDetailOfOrder frm = new FrmDetailOfOrder(int.Parse(dtgvData.SelectedRows[0].Cells["ID"].Value.ToString()));
                frm.ShowDialog();
            }
        }

        private void dtgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dtgvData.Rows[e.RowIndex].Cells[2].Value.ToString().ToUpper() == "SUM")
            {
                e.CellStyle.BackColor = Color.Crimson;
                e.CellStyle.ForeColor = Color.White;
            }
        }
	}
}